<%@ page import="BusinessLogic.ControllerFacade" %>
<%@ page import="Presentation.WebUI.LinksGenerator" %>
<%@ page import="Domain.DataStructures.IMovieData" %>
<%@ page import="Domain.Actor" %>
<%@ page import="Domain.Genre" %>
<%@ page import="Domain.DataStructures.IRecommendationData" %>
<%@ page import="Domain.DataStructures.IUserData" %>
<%@ page import="java.util.Collection" %>
<%@ page errorPage="error.jsp" %>

<%
    ControllerFacade c = (ControllerFacade) session.getAttribute("c");
    String movieName = request.getParameter("movieName");
    IMovieData movie = c.getMovie(movieName);
    String res;

    int amount = c.getListLength();
    int start = 0;
    String startParam = request.getParameter("start");
    if (startParam != null)
        start = Integer.parseInt(startParam);
%>

<h2><span class="movie"><%=movie.getName()%></span></h2>


<table>
<%
    try {
        IUserData loggedUser = c.getCurrentUser();
        if (loggedUser.isAdministrator()) {
%>
    <tr>
        <td colspan="2" id="delete">
            <a href="index.jsp?action=DeleteMovie&movieName=<%= movie.getName()%>">Delete movie</a>
        </td>
    </tr>
<%
    }
}
catch(Exception e){

}
%>

    <tr>
        <td class="title">
            <label title="countrry">Country</label>
        </td>
        <td>
            <%=LinksGenerator.searchLink(movie.getCountry(),"COUNTRY")%>
        </td>
    </tr>
    <tr>
        <td>
            <label title="year">Year</label>
        </td>
        <td>
            <%=LinksGenerator.searchLink(movie.getYear(),"YEAR")%>
        </td>
    </tr>
    <tr>
        <td>
            <label title="length">Length</label>
        </td>
        <td>
            <%=movie.getLength()%>
        </td>
    </tr>
    <tr>
        <td>
            <label title="director">Director</label>
        </td>
        <td>
            <%= "<span class=\"director\">" + LinksGenerator.searchLink(movie.getDirector(),"DIRECTOR") + "</span>"%>
        </td>
    </tr>
    <tr>
        <td valign="top">
            <label title="actors">Actors</label>
        </td>
        <td>
            <%
                res = "";
                for (Actor actor : movie.getActors()) {
                    if (!res.equals("")) res += ", ";
                    res += "<span class=\"actor\">" + LinksGenerator.searchLink(String.valueOf(actor.getId()),actor.getName(), "ACTOR") + "</span>";
                }
                out.println(res);
            %>
        </td>
    </tr>
    <tr>
        <td valign="top">
            <label title="genres">Genres</label>
        </td>
        <td>
            <%
                res = "";
                for (Genre genre : movie.getGenres()) {
                    if (!res.equals("")) res += ", ";
                    res += "<span class=\"genre\">" + LinksGenerator.searchLink(String.valueOf(genre.getId()),genre.getGenre(), "GENRE") + "</span>";
                }
                out.println(res);
            %>
        </td>
    </tr>
    <tr>
        <td valign="top" colspan="2">
            <table>
                <tr>
                    <td><h3>Recommendations</h3></td>
                </tr>
                <tr>
                    <td align="center">
                        <%
                            String rec = "";
                            Collection recs = c.getRecommendationForMovie(movieName, start);

                            if (start == 0 && recs.size() == 0) {
                                out.println("Movie not yet recommended!");
                            }
                            else {
                                String titles = "<tr>";
                                titles += "<th class=\"userColumn\">User</th>";
                                titles += "<th class=\"rankColumn\">Rank</th>";
                                titles += "<th class=\"descColumn\">Description</th>";
                                titles += "</tr>";

                                String[] show;
                                if (recs.size() > 0) {
                                    show = new String[recs.size() + 1];
                                    show[0] = titles;
                                } else
                                    show = new String[0];

                                int i = 1;
                                for (Object rd : recs) {
                                    rec = "<tr class=\"recommendation\">";

                                    rec += "<td class=\"username\">" + LinksGenerator.userLink(((IRecommendationData) rd).getUserName()) + "</td>";
                                    rec += "<td class=\"rank\">" + (((IRecommendationData) rd).getUserRank()) + "</td>";
                                    rec += "<td class=\"description\">" + (((IRecommendationData) rd).getDescription()) + "</td>";

                                    rec += "</tr>";

                                    show[i] = rec;
                                    i++;
                                }
                                String last = "No more recommendations.";

                                rec = LinksGenerator.showTable(show, request.getParameterMap(), start, amount, last);
                                out.println(rec);
                            }
                        %>
                    </td>
                </tr>
            </table>
        </td>
    </tr>
    <tr>
		<td valign="top" colspan="2">
			<%
                if (recs.size() > 0)
                    out.print(LinksGenerator.navLinks(request.getParameterMap(), start, amount));
                else
                    out.print(LinksGenerator.navLink(request.getParameterMap(), start - amount, "prev"));
            %>
		</td>
	</tr>
</table>

